package core.manager
{
import core.Language;
import core.model.GameModel;
import core.model.MapModel;
import core.model.PlayerModel;
import core.model.QuestModel;

import mx.utils.StringUtil;

import utils.TimeUtil;

import values.item.BuildingItemValue;
import values.item.building.BuildingUpgradeRequirementValue;
import values.other.ConditionValue;
import values.quest.QuestTemplateValueList;
import values.shop.ShopItemValue;

/**
 * RequirementManager
 * @author jliang
 *
 */
public class RequirementManager
{
	private static function get playerModel():PlayerModel
	{
		return GameModel.instance.playerModel;
	}
	
	private static function get mapModel():MapModel
	{
		return GameModel.instance.mapModel;
	}
	
	private static function get questModel():QuestModel
	{
		return GameModel.instance.questModel;
	}
    
    public static function checkItemBuyRequirement(shopItem:ShopItemValue):ConditionValue
    {
        var con:ConditionValue = new ConditionValue();
        con.isReached = true;
        
        //判断商品是否已下架
        if (shopItem.hasTimeOff)
        {
            if (TimeUtil.currentGameTimeSec > shopItem.timeOff)
            {
                con.desc = Language.getText("@shop_buyable_check_label_4");
                con.isReached = false;
                return con;
            }
        }
        
        //判断商品是否已售完
        if (shopItem.hasDailyLimit)
        {
            if (shopItem.todayRestNum <= 0)
            {
                con.desc = Language.getText("@shop_buyable_check_label_3");
                con.isReached = false;
                return con;
            }
        }
        
        //判断商品是否已达今天购买上限
        if (shopItem.hasCountLimit)
        {
            if (shopItem.totalRestNum <= 0)
            {
                con.desc = Language.getText("@shop_buyable_check_label_5");
                con.isReached = false;
                return con;
            }
        }
        
        //判断城堡等级
        if (shopItem.itemValue is BuildingItemValue)
        {
            if (mapModel.castleLevel < shopItem.itemValue.template.requirements.cityLevelRequired)
            {
                con.desc = Language.getText("@shop_buyable_check_label_1");
                con.isReached = false;
                return con;
            }
        }
        
        //判断是否有足够的消耗
        if (!CostManager.hasEnoughToCost(shopItem.nowCost, false))
        {
            con.desc = Language.getText("@shop_buyable_check_label_2");
            con.desc = StringUtil.substitute(con.desc, shopItem.nowCost.costValueList[0].name);
            con.isReached = false;
            return con;
        }
        
        return con;
    }
	
	/**
	 * { cons: Vector.<ConditionValue>, isReached: Boolean } 
	 * @param vo
	 * @return 
	 * 
	 */	
	public static function checkUpgradeRequirement(vo:BuildingUpgradeRequirementValue):Object
	{
		var cons:Vector.<ConditionValue> = new Vector.<ConditionValue>();
		
		var isReached:Boolean = true;
		
		var con:ConditionValue;
		if (vo.hasCityLevelRequired)
		{
			con = new ConditionValue();
			con.desc = StringUtil.substitute(Language.getText("@building_upgrade_city_level_requirement_label"), vo.cityLevelRequired);
			//判断城堡等级
			con.isReached = vo.cityLevelRequired <= mapModel.castleLevel;
			cons.push(con);
			isReached = isReached && con.isReached;
		}
		
		if (vo.hasLevelRequired)
		{
			con = new ConditionValue();
			con.desc = StringUtil.substitute(Language.getText("@building_upgrade_level_requirement_label"), vo.levelRequired);
			con.isReached = (playerModel.myPlayerAvatar.level >= vo.levelRequired);
			cons.push(con);
			isReached = isReached && con.isReached;
		}
		
		if (vo.hasQuestRequired)
		{
			con = new ConditionValue();
			var qlist:Array = vo.questRequired.split(",");
			var quests:String = "";
			var questDone:Boolean = false;
			for each (var qkey:String in qlist)
			{
				qkey = StringUtil.trim(qkey);
				quests += QuestTemplateValueList.getByKey(qkey).name + ", ";				
				questDone = questDone && questModel.hasQuestCompleted(qkey);
			}
			quests = quests.substr(0, quests.length - 2);
			con.desc = StringUtil.substitute(Language.getText("@building_upgrade_quest_requirement_label"), quests);
			con.isReached = questDone;
			cons.push(con);
			isReached = isReached && con.isReached;
		}
				
		return { cons: cons, isReached: isReached };
	}
}
}



